﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Modules.DOCUMENT;
using System.Collections;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.Configuration;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.MENU
{
    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            ModuleSettingItem itemSetting = new ModuleSettingItem(new StringDataType());
            itemSetting.Value = Guid.Empty.ToString();
            itemSetting.Name = "ModuleID của Module DOCUMENT";
            itemSetting.Description = "ModuleID của Module DOCUMENT";
            itemSetting.SortOrder = 1;
            this.BaseSettings.Add("DOCUMENT.ModuleID", itemSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable settings = this.Settings;
            Guid moduleID = new Guid(settings["DOCUMENT.ModuleID"].ToString());

            Hashtable hashtable = PortalConfiguration.GetModuleSettings(moduleID);

            // Left Pane.
            string url = string.Empty;
            LiteralControl literal = new LiteralControl("");
            literal.Text += "<div class='MenuLink'>";
            try
            {
                if (Convert.ToBoolean(hashtable["MultipleCategories"].ToString()))
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.SearchWithMultipleCategories));
                }
                else
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Search));
                }
            }
            catch
            {
                url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Search));
            }
            
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Tìm kiếm văn bản</a>", url);

            try
            {
                if (Convert.ToBoolean(hashtable["MultipleCategories"].ToString()))
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.ItemWithMultipleCategories, "optionview=None"));
                }
                else
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=None"));
                }
            }
            catch
            {
                url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=None"));
            }

            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Văn bản mới cập nhật</a>", url);

            try
            {
                if (Convert.ToBoolean(hashtable["MultipleCategories"].ToString()))
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.ItemWithMultipleCategories, "optionview=PublishedDate"));
                }
                else
                {
                    url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=PublishedDate"));
                }
            }
            catch
            {
                url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=PublishedDate"));
            }

            
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Văn bản mới ban hành</a>", url);

            literal.Text += "</div>";
            // Manager Permission.
            if (PortalSecurity.HasManageAuthorization(moduleID))
            {
                literal.Text += "<div class='MenuLinkGroup'>Quản trị nội dung</div>";
                literal.Text += "<div class='MenuLink'>";
                // Add new item.
                try
                {
                    if (Convert.ToBoolean(hashtable["MultipleCategories"].ToString()))
                    {
                        url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "AddEditItemWithMultipleCategories");
                    }
                    else
                    {
                        url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "AddEditItem");
                    }
                }
                catch 
                {
                    url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "AddEditItem");
                }
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Tạo mới văn bản</a>", url);
                // Category.
                url = HttpUrlBuilder.BuildShareUrl(moduleID, 0, "CategoryManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Lĩnh vực</a>", url);
                // Publisher.
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "PublisherManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Cơ quan ban hành</a>", url);
                // Type.
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "TypeManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Loại văn bản</a>", url);
                // Document.
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "ItemManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Văn bản</a>", url);

                literal.Text += "</div>";
            }

            IList<Category> allCategories = new List<Category>();
            allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", topCategory.ID);
                url = url.Substring(2);
                literal.Text += string.Format("<div class='MenuLinkGroup'><a href='{0}'>{1}</a></div>", url, topCategory.Title);
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();
                if (childCategories.Count > 0)
                {
                    literal.Text += "<div class='MenuLink'>";

                    foreach (Category childCategory in childCategories)
                    {
                        url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", childCategory.ID);
                        url = url.Substring(2);

                        literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» {1}</a>", url, childCategory.Title);
                    }
                    literal.Text += "</div>";
                }
            }
            literal.Text += "</div>";
            ContentPane.Controls.Add(literal);
        }
    }
}